/**
 * Created by Freeman on 2016/11/30.
 */
import fetchJsonp from 'fetch-jsonp';
import {observable, computed, action} from 'mobx';
const SINGLE = 0
const SEQUENTIAL = 1
const RANDOM = 2
export default class MusicStore {
  @observable music

  constructor() {
    this.music = {
      playing: true,
      currentTime: 0,
      duration: 0,
      playMode: SEQUENTIAL,
      index: 0,
      coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003RMaRI1iFoYd.jpg?max_age=2592000',
      song: {
        id: '107192078',
        mid: '003OUlho2HcRHC',
        name: '告白气球',
        singer: '周杰伦'
      },
      playList: [{
        id: '107192078',
        mid: '003OUlho2HcRHC',
        name: '告白气球',
        singer: '周杰伦'
      }, {
        id: "5106429",
        mid: "000QwTVo0YHdcP",
        name: "丑八怪",
        singer: "薛之谦"
      }]
    }

  }

  // get music src
  @computed get dataUrl() {
    return 'http://ws.stream.qqmusic.qq.com/' + this.music.song.id + '.m4a?fromtag=46'
  }

  @computed get indicatorPosition() {
    return this.music.currentTime / this.music.duration * 100
  }

  @computed get currentTime() {
    return parseInt(this.music.currentTime / 60) + ':' + (Array(2).join(0) + (this.music.currentTime % 60)).slice(-2)
  }

  @computed get duration() {
    return parseInt(this.music.duration / 60) + ':' + (Array(2).join(0) + (this.music.duration % 60)).slice(-2)
  }

  @action getMid() {
    fetchJsonp('http://120.27.93.97/weappserver/get_music_image.php?mid=' + this.music.song.mid,
      {
        method: 'GET',
        mode: 'no-cors',
        cache: 'default'
      })
      .then(res => res.json())
      .then((response) => {
        this.music.coverImgUrl = response.url
      }).catch(e => {
      console.log(e)
    })
  }

  // play or pause audio
  play() {
    this.music.playing = !this.music.playing
  }

  playFront() {
    this.music.index = (this.music.index - 1 + this.music.playList.length) % this.music.playList.length
    this.music.song = this.music.playList[this.music.index]
    this.getMid()
  }

  playNext() {
    this.music.index = (this.music.index + 1) % this.music.playList.length
    this.music.song = this.music.playList[this.music.index]
    this.getMid()
  }

  updateCurrentTime(time) {
    this.music.currentTime = time
  }

  updateDuration(time) {
    this.music.duration = time
  }

  playContinue() {
    switch (this.music.playMode) {
      case SINGLE:
        break
      case SEQUENTIAL:
        this.music.index = (this.music.index + 1) % this.music.playList.length
        this.music.song = this.music.playList[this.music.index]
        this.getMid()
        break
      case RANDOM:
        this.music.index = Math.floor(Math.random() * this.music.playList.length)
        this.music.song = this.music.playList[this.music.index]
        this.getMid()
        break
    }
  }
  addToPlayList(item){
    this.music.playList.push(item)
  }
  addToPlayListAsNextPlay (item) {
    this.music.playList.splice(this.music.index + 1, 0, item)
  }
}
